package com.liudy.demo.asyncinvoke;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Future;

@Configuration
@EnableAsync
public class AsyncInvoke {

    /**
     * 模拟长耗时操作
     */
    @Async
    public void longTimeOpreation() throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name);
        Thread.currentThread().sleep(1000 * 5);
        System.out.println("睡眠结束");
    }

    @Async
    public Future<String> longTimeFuture() throws Exception {
        Thread.currentThread().sleep(1000 * 5);
        int a = 5/0;
        return new AsyncResult<String>("hello world !!!!");
    }

}